﻿using UnityEngine;
using System.Collections;
using ProtoBuf.Message;
using System.Collections.Generic;

namespace UI
{
    public class UICopyRunMap :Page
    {
        public UICopyRunMapTopInfo topInfo;
        public UICopyRunMapHeroInfo heroInfo;
        
        public GameObject btnExit;

        public static bool isUICopyRunMap;

        public bool enableEffect = false;
        public float fadeSpeed = 20f;
        protected override void DoOpen()
        {
            Init();

			if (!LoadingPanel.Instance.isLoading)
				UIAutoSize.Instance.ShowMaskUI(false);
        }

        public override void DoReopen()
        {
            if (StaticData.isCompleteTask)
            {
                topInfo.gameObject.SetActive(false);
                heroInfo.gameObject.SetActive(false);
                StaticData.willBaseDrops.Clear();
                //增加挑战次数
                if (StaticData.isLastBattleWin)
                {
                    UI.UICopyData.AddCurrentLevelChallengeCount(StaticData.currFightLevelId);
                }
                return;
            }

            topInfo.gameObject.SetActive(true);
            heroInfo.gameObject.SetActive(true);
            InitCopyInfo();
            var msg = StaticData.lastBattleEndMsg as ProtoBuf.Message.SCCopyFightEnd;
            if (msg != null && msg.currentResource.Count > 0)
            {
                ShowCopySmallBalance();
            }
        }
        
        public override void OnForceClose()
        {
            isUICopyRunMap = false;
        }

        public void Init()
        {
            isUICopyRunMap = true;
            LoadingPanel.Instance.ShowLoading();
            StaticData.isCompleteTask = false;
            Room.RoomManager.Switch(SceneType.COPY,null,true);
            
            InitCopyInfo();
            if(GuideManager.Instance!=null)
            {
                if (GuideManager.Instance.GetGuideOrder() <= 9)
                {
                    btnExit.SetActive(false);
                }
                else
                {
                    btnExit.SetActive(true);
                }
            }
        }

        public void PlayIntoBattleEffect()
        {
            enableEffect = true;
        }

        public void EndIntoBattleEffect()
        {
            enableEffect = false;
        }

		protected override void DoClose ()
		{
			base.DoClose ();
			UIAutoSize.Instance.ShowMaskUI(true);
		}

        void Update()
        {
            if(enableEffect)
            {
                var cam = CameraTool.Instance.sceneCamera;
                cam.fieldOfView -= Time.deltaTime* fadeSpeed;
            }
        }

        public void ShowCopySmallBalance() {
            var obj = AssetLoad.ResourceManager.Instance.LoadResource("Pages/CopySmallBalanceExt") as GameObject;
            if (obj != null)
            {
                var battleBalance = GameObject.Instantiate(obj) as GameObject;
                battleBalance.transform.SetParent(transform);
                battleBalance.transform.localPosition = Vector3.zero;
                battleBalance.transform.localScale = Vector3.one;
                battleBalance.transform.localEulerAngles = Vector3.zero;
                battleBalance.GetComponent<Battle.CopySmallBalance>().Init();
                battleBalance.GetComponent<Battle.CopySmallBalance>().PlayAni();
            }
        }
        
        #region event button
        public void OnExitClick()
        {

            BoxManager.CreateMessageBoxWithCofirmAndCancelButton(TableTextBlo.GetText("exit_copy_tip"), true,(b) =>
            {
                if(b)
                {
                    var roomCopy = Room.RoomManager.currentRoom as Room.RoomCopy;
                    if (roomCopy != null)
                    {
                        roomCopy.Exit();
                    }
                    StaticData.willBaseDrops.Clear();
                    UIAutoSize.Instance.ShowMaskUI(true);
                }
                else
                {

                }
            }, TableTextBlo.GetText("exit_copy_tip_title"));
        }

        public void AutoPlayButton()
        {

        }


        public void Box(List<BaseResource> list)
        {
            ManagerController.Instance.StartCoroutine(_DelayOpenBox(list));
        }

        IEnumerator _DelayOpenBox(List<BaseResource> list)
        {
            yield return new WaitForSeconds(1.5f);
            var box = AssetLoad.ResourceManager.Instance.LoadResource("Pages/CopySmallBalanceExt") as GameObject;
            var battleBalance = GameObject.Instantiate(box) as GameObject;
            battleBalance.transform.SetParent(transform);
            battleBalance.transform.localPosition = Vector3.zero;
            battleBalance.transform.localScale = Vector3.one;
            battleBalance.transform.localEulerAngles = Vector3.zero;
            battleBalance.SetActive(true);
            battleBalance.GetComponent<UICopySmallBalanceExt>().Init(list);
            battleBalance.GetComponent<UICopySmallBalanceExt>().PlayAni();
            StaticData.willBaseDrops.AddRange(list);
        }

        public void BoxButton()
        {
            
            if (StaticData.willBaseDrops.Count > 0)
            {
                var box = AssetLoad.ResourceManager.Instance.LoadResource("Pages/CopySmallBalanceExt") as GameObject;
                var battleBalance = GameObject.Instantiate(box) as GameObject;
                battleBalance.transform.SetParent(transform);
                battleBalance.transform.localPosition = Vector3.zero;
                battleBalance.transform.localScale = Vector3.one;
                battleBalance.transform.localEulerAngles = Vector3.zero;
                battleBalance.SetActive(true);
                battleBalance.GetComponent<UICopySmallBalanceExt>().Init(StaticData.willBaseDrops);
                battleBalance.GetComponent<UICopySmallBalanceExt>().PlayAni();
            }
            else
            {
                BoxManager.CreatePopupTextBox(TableTextBlo.GetText("copy_has_not_items"));
            }
        }
        #endregion

        #region 请求一个协议
        public void InitCopyInfo()
        {
            CSCopyRunMapInfo info = new CSCopyRunMapInfo();
            info.chapterId = StaticData.currFightChapterId;
            info.missionId = StaticData.currFightLevelId;

            NetAPI.SendHttp(OpDefine.CSCopyRunMapInfo, info, (obj, ok) => 
            {
                if(ok)
                {
                    topInfo.Init(obj.kBody);
                    heroInfo.Init(obj.kBody);
                }
            });

           
        }
#endregion

    }
}